
// console.log('\n\n 查看 gulp 插件包含什么--->\n\n',$,'\n\n');


const gulp = require('gulp'), 
    del = require('del'),
    runSequence = require('run-sequence'),
    $ = require('gulp-load-plugins')();


gulp.task('clean', del.bind(null, ['./dist','./rev'], {
    force: true
}));

gulp.task('css', function () {

    return  gulp.src(['src/stylus/index.styl', 'src/stylus/test.styl']) 
            .pipe($.stylus()) // 该任务调用的模块
            .pipe($.autoprefixer({
                browsers: ['last 10 versions', 'Android >= 4.0'],

                // 是否美化属性值 默认：true 像这样：
                // -webkit-transform: rotate(45deg);
                //        transform: rotate(45deg);
                cascade: true, 
                remove: false // 是否去掉不必要的前缀 默认：true 
            }))
            .pipe($.concat("index.css"))
            .pipe($.minifyCss())
            .pipe($.rev())
            .pipe(gulp.dest('dist/css'))

            .pipe( $.rev.manifest() )
            .pipe( gulp.dest( 'rev/css' ) );

});

gulp.task('js', function () {

    return  gulp.src(['src/js/test.js', 'src/js/index.js']) 
            .pipe($.babel({
                presets: ['es2015']
            })) 
            .pipe($.concat("index.js"))
            .pipe($.uglify())
            .pipe($.rev())
            .pipe(gulp.dest('dist/js'))
            
            .pipe( $.rev.manifest() )
            .pipe( gulp.dest( 'rev/js' ) );

});

gulp.task('img',function (params) {
    return gulp.src('src/img/*')
            .pipe($.imagemin())
            .pipe(gulp.dest('dist/img'))
})

gulp.task('html', function () {

    var options = {
        removeComments: true,// 清除HTML注释
        collapseWhitespace: true,// 压缩HTML
        collapseBooleanAttributes: true,// 省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,// 删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,// 删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,// 删除<style>和<link>的type="text/css"
        minifyJS: true ,// 压缩页面JS
        minifyCSS: true// 压缩页面CSS
    };

    

    var sources = gulp.src([ './dist/js/*.js', './dist/css/*.css'], { read: false });

    return  gulp.src(['rev/**/*.json',"src/test.html"])
            .pipe($.inject(sources))
            .pipe($.revCollector({
                replaceReved: true,
                dirReplacements: {
                    '/dist/css/': 'css/',
                    '/dist/js/': 'js/'
                }
            }))

            // .pipe($.revAppendAll()) 暂时没用
            .pipe($.htmlmin(options))
            .pipe(gulp.dest("dist"))

});

gulp.task('default',function (cb) {

    // 同步运行任务
    runSequence('clean', ['css', 'js','img'],'html',cb)
    
})
